package org.shuma.productPriceType.impl;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.shuma.productPriceType.model.dto.PriceTypeDTO;
import org.shuma.productPriceType.model.dto.ProductPriceTypeDTO;
import org.shuma.productPriceType.model.entity.PriceType;
import org.shuma.productPriceType.mapper.PriceTypeMapper;
import org.shuma.productPriceType.mapper.ProductPriceTypeMapper;
import org.shuma.productPriceType.domain.entity.ProductPriceType;
import org.shuma.productPriceType.facade.ProductPriceTypeFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@SofaService(interfaceType = ProductPriceTypeFacade.class, uniqueId = "productPriceType99", bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class ProductPriceTypeImpl extends ServiceImpl<ProductPriceTypeMapper,ProductPriceType>
        implements ProductPriceTypeFacade {
    @Autowired
    ProductPriceTypeMapper productPriceTypeMapper;

    @Autowired
    PriceTypeMapper priceTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertppt_Type(ProductPriceTypeDTO ppt) {
        //有纯现金的兑换方式
        if(ppt.getPptMoney() != null) {
            PriceType priceType = new PriceType(ppt.getPptMoney(),0);
            priceType.setPptType(0);

            priceTypeMapper.insert(priceType);
            save(new ProductPriceType(ppt.getProductId(),priceType.getPptId()));
        }
        //有纯积分兑换方式
        if(ppt.getPptIntegral() != null) {
            PriceType priceType = new PriceType(0,ppt.getPptIntegral());
            priceType.setPptType(1);

            priceTypeMapper.insert(priceType);
            save(new ProductPriceType(ppt.getProductId(),priceType.getPptId()));
        }
        //有混合兑换方式
        if(ppt.getPptMoneyIntegral() != null) {
            List<Integer> list = ppt.getPptMoneyIntegral();
            PriceType priceType = new PriceType(list.get(0),list.get(1));
            priceType.setPptType(2);

            priceTypeMapper.insert(priceType);
            save(new ProductPriceType(ppt.getProductId(),priceType.getPptId()));
        }
        return true;
    }

    @Override
    public List<PriceType> getppt_Type(Integer productId) {
        List<ProductPriceType> products = productPriceTypeMapper.selectListById(productId);

        List<PriceType> list = new ArrayList<PriceType>();

        for (ProductPriceType productPriceType : products) {
            Long id = productPriceType.getPriceTypeId();

            PriceType priceType = priceTypeMapper.selectOne(
                    new QueryWrapper<PriceType>().eq("ppt_id", id).eq("is_delete",0));
            list.add(priceType);
        }
        return list;
    }

    @Override
    public boolean updateppt_Type(PriceTypeDTO ppt) {
        PriceType priceType = new PriceType(ppt.getPptMoney(), ppt.getPptIntegral());
        LambdaQueryWrapper<PriceType> queryWrapper = new LambdaQueryWrapper<PriceType>()
                .eq(PriceType::getPptId,ppt.getPptId());

        try {
            priceTypeMapper.update(priceType,queryWrapper);
        }catch (Exception exception) {
            return false;
        }
        return true;
    }
}

